#pragma once
#include "socked.hpp"
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#define MAX_NUM_FD 1024
#define DEFAULT_DATA -1

class SelectServe
{
public:
    SelectServe()
        : socket_(8888)
    {
        for (int i = 0; i < MAX_NUM_FD; i++)
        {
            fd_arr[i] = DEFAULT_DATA;
        }
    }

    ~SelectServe()
    {
    }

    void init()
    {
        socket_.Socket();
        socket_.Bind();
        socket_.Listen();
    }

    int get_max()
    {
        int ret = fd_arr[0];
        for (int i = 1; i < MAX_NUM_FD; i++)
        {
            if (fd_arr[i] != DEFAULT_DATA)
            {
                if (ret < fd_arr[i])
                    ret = fd_arr[i];
            }
        }
        return ret;
    }

    void deal_fd()
    {
        //遍历描述符数组
        for(int i = 0; i < MAX_NUM_FD; i++)
        {
            if(FD_ISSET(fd_arr[i], &rfds))
            {
                if(fd_arr[i] == socket_.sockfd_)
                {
                    //处理连接请求
                    socket_.Accept();
                        //添加描述符
                    for(int j = 0; j < MAX_NUM_FD; j++)
                    {
                        if(fd_arr[j] == DEFAULT_DATA)
                        {
                            fd_arr[j] = socket_.lis_sockfd_;
                        }
                    }
                }
                else
                {
                    //接收数据
                    char buf[1024];
                    int n = read(fd_arr[i], buf, sizeof(buf));
                    if(n > 0)
                    {
                        buf[n] = 0;
                        cout << buf << endl;
                    }
                    else if(n == 0)
                    {
                        log(Warning, "stop connect");
                        close(fd_arr[i]);
                        fd_arr[i] = DEFAULT_DATA;
                    }
                    else
                    {
                        log(Warning, "read fail");
                        close(fd_arr[i]);
                        fd_arr[i] = DEFAULT_DATA;
                    }
                }
            }
        }
    }
    void start()
    {
        while (1)
        {
            fd_arr[0] = socket_.sockfd_;
            FD_ZERO(&rfds);
            //设置FD
            for(int i = 0; i < MAX_NUM_FD; i++)
            {
                if(fd_arr[i] != DEFAULT_DATA)
                    FD_SET(fd_arr[i], &rfds);
            }

            struct timeval time{4,0};
            int n = select(get_max() + 1, &rfds, nullptr, nullptr, &time);
            if (n < 0)
            {
                log(Warning, "select fail");
            }
            else if(n == 0)
            {
                log(Debug, "wait");
            }
            else
            {
                log(Debug, "select success: %d", n);
            }

            // 处理事件
            deal_fd();
        }
    }

    Sock socket_;
    fd_set rfds;
    int fd_arr[MAX_NUM_FD]; // 文件描述符数组
};